package com.qfjg.webcore.exception;

import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;

@RestControllerAdvice
public class ExceptionInterceptor {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<UniformError> handle(Exception e) {
        if(e instanceof UnauthorityError) {
            return ResponseEntity.status(401).
                  cacheControl(CacheControl.noCache())
                  .contentType(MediaType.APPLICATION_JSON)
                  .body(new UniformError(
                        new Date(),
                        e.getClass().getName(),
                        e.getMessage()));
        }
        else{
            return ResponseEntity
                  .badRequest()
                  .cacheControl(CacheControl.noCache())
                  .contentType(MediaType.APPLICATION_JSON)
                  .body(new UniformError(
                        new Date(),
                        e.getClass().getName(),
                        e.getMessage()));
        }
    }

}
